#!/bin/bash
#hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $NF }')
hostip='127.0.0.1'
# 协议
protocol='http'
#protocol='socks5'
# 端口
port='7890'

PROXY_HTTP="${protocol}://${hostip}:${port}"

on_proxy(){
    export http_proxy="${PROXY_HTTP}"
    export HTTP_PROXY="${PROXY_HTTP}"
    export https_proxy="${PROXY_HTTP}"
    export HTTPS_proxy="${PROXY_HTTP}"

    git config --global http.proxy ${PROXY_HTTP}
    git config --global https.proxy ${PROXY_HTTP}

    # 使用 proxychains 的话，取消下面这行的注释
    # sudo sed /etc/proxychains.conf -i -e "s/^socks.*$/${protocol}:\/\/${hostip}:${port}/"
}

off_proxy(){
    unset http_proxy
    unset HTTP_PROXY
    unset https_proxy
    unset HTTPS_PROXY

    git config --global --unset http.proxy
    git config --global --unset https.proxy
}

if [ "$1" = "on" ]; then
    on_proxy
    echo "on proxy"
    echo ${PROXY_HTTP}
elif [ "$1" = "off" ]; then
    off_proxy
    echo "off proxy"
else
    echo "input error"
fi
